package com.dc.service.impl;

import com.dc.dao.CategoryDao;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.Category;
import com.dc.service.CategoryService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryDao categoryDao;
    public BaseModel selectPageCategory(int curr_page, int page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<Category> categories = categoryDao.selectPageCategory();
        PageInfo pageInfo = new PageInfo(categories,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分页查询分类信息成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel selectCategory(int id, BaseModel baseModel) throws Exception {
        Category category = categoryDao.selectCategotyById(id);
        baseModel.setResultCode(0);
        baseModel.setMessage("根据id查询成功");
        baseModel.setData(category);
        return baseModel;
    }

    public BaseModel addCategory(Category category, BaseModel baseModel) throws Exception {
        int result = categoryDao.insertCategory(category);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_SHOES_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增成功");
        return baseModel;
    }

    public BaseModel deletedCategory(String ids, BaseModel baseModel) throws Exception {
        String[] idArry = ids.split(",");
        int result = categoryDao.deletedCategory(idArry);
        if(result <=0 ){
            throw new BusinessException(ErrorMessageEnum.DELETED_SHOES_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"条记录成功");
        return baseModel;
    }

    public BaseModel updateCategory(Category category, BaseModel baseModel) throws Exception {
        int result = categoryDao.updateCategory(category);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.UPDATE_SHOES);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改分类成功");
        return baseModel;
    }
}
